﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService;

public partial class administration_MergeCompanies : System.Web.UI.Page
{
    ModelAdapter service = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        service = new ModelAdapter();
    }

    private void FirstGridBind(int companyID)
    {
        firstGridView.DataSource = service.GetReviewForMergeCompanies(companyID);
        firstGridView.DataBind();
    }

    private void SecondGridBind(int companyID)
    {
        secondGridView.DataSource = service.GetReviewForMergeCompanies(companyID);
        secondGridView.DataBind();
    }
    protected void SFindID_Click(object sender, EventArgs e)
    {

        int fcompanyID;
        if (int.TryParse(firstCompany.Value, out fcompanyID))
        {
            var company = service.GetCompanyByID(fcompanyID);
            if (company != null)
            {
                firstCompanyImage.Src = string.IsNullOrEmpty(company.Logo) ? "upload/no-company-logo.jpg" : "upload/" + company.Logo;
                firstCompanyImage.Visible = true;
                firstCompanyLink.HRef = "~/SingleCompany.aspx?id=" + company.CompanyID;
                firstCompanyLink.InnerText = company.Name;
                firstCompanyLink.Visible = true;
                FirstGridBind(fcompanyID);
            }
            else
            {
                firstCompanyImage.Visible = false;
                firstCompanyLink.Visible = false;
            }
        }

        int scompanyID;
        if (int.TryParse(secondCompany.Value, out scompanyID))
        {
            var company = service.GetCompanyByID(scompanyID);
            if (company != null)
            {
                secondCompanyImage.Src = string.IsNullOrEmpty(company.Logo) ? "upload/no-company-logo.jpg" : "upload/" + company.Logo;
                secondCompanyImage.Visible = true;

                secondCompanyLink.HRef = "~/SingleCompany.aspx?id=" + company.CompanyID;
                secondCompanyLink.InnerText = company.Name;
                secondCompanyLink.Visible = true;
                SecondGridBind(scompanyID);
            }
            else
            {
                secondCompanyImage.Visible = false;
                secondCompanyLink.Visible = false;
            }
        }

    }

    protected void MoveLeft_Click(object sender, EventArgs e)
    {
        var firstcompanyId = int.Parse(firstCompany.Value);
        var secondcompanyId = int.Parse(secondCompany.Value);
        foreach (GridViewRow gvr in secondGridView.Rows)
        {
            CheckBox chk = (CheckBox)gvr.Cells[0].FindControl("CheckBoxRight");
            var reviewid = int.Parse(gvr.Cells[1].Text);
            if (chk.Checked)
            {
                service.changeReviewCompanyByID(reviewid, firstcompanyId);
            }
        }
        FirstGridBind(firstcompanyId);
        SecondGridBind(secondcompanyId);

    }
    protected void MoveRight_Click(object sender, EventArgs e)
    {
        var firstcompanyId = int.Parse(firstCompany.Value);
        var secondcompanyId = int.Parse(secondCompany.Value);
        foreach (GridViewRow gvr in firstGridView.Rows)
        {
            CheckBox chk = (CheckBox)gvr.Cells[0].FindControl("CheckBoxLeft");
            var reviewid = int.Parse(gvr.Cells[1].Text);
            if (chk.Checked)
            {
                service.changeReviewCompanyByID(reviewid, secondcompanyId);
            }
        }
        FirstGridBind(firstcompanyId);
        SecondGridBind(secondcompanyId);
    }

    protected void DeletefirstCompany_Click(object sender, EventArgs e)
    {
        int cID = 0;
        int sID = int.Parse(secondCompany.Value);
        if (int.TryParse(firstCompany.Value, out cID))
        {
            service.DeleteCompanyAndAllReviewByID(cID);
        }
        FirstGridBind(cID);
        SecondGridBind(sID);
    }
    protected void DeleteSecondCompany_Click(object sender, EventArgs e)
    {
        int cID = 0;
        int sID = int.Parse(firstCompany.Value);
        if (int.TryParse(secondCompany.Value, out cID))
        {
            service.DeleteCompanyAndAllReviewByID(cID);
        }
        FirstGridBind(sID);
        SecondGridBind(cID);
    }
}